package com.gph.saviorframework.util;

import com.gph.saviorframework.FieldType;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


public class RequestUtil {

    public static Map<String, Object> getQueryMap(HttpServletRequest request, Map<String, String> fields) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(null == fields){
            return  null;
        }
        try {
            for (String field : fields.keySet()) {
                String object = request.getParameter(field);
                if (object == null) {
                    continue;
                }
                String value = "";
                if (getEncoding(object).toLowerCase().equals("gb2312") || getEncoding(object).toLowerCase().equals("utf-8")
                        || getEncoding(object).toLowerCase().equals("gbk")) {
                    value = object;
                } else {
                    value = new String(object.getBytes("iso-8859-1"), "utf-8");
                }

                //ServletRequestUtils.getStringParameter(request, field);
                if (null != value && !value.isEmpty()) {
                    if (fields.get(field).equals(FieldType.STRING)) {
                        map.put(field, "%" + value + "%");
                    } else if (fields.get(field).equals(FieldType.INTEGER)) {
                        map.put(field, Integer.valueOf(value));
                    } else if (fields.get(field).equals(FieldType.DOUBLE)) {
                        map.put(field, Double.valueOf(value));
                    } else if (fields.get(field).equals(FieldType.DATE)) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        map.put(field, format.parse(value));
                    } else if (fields.get(field).equals(FieldType.TIME)) {
                        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                        map.put(field, format.parse(value));
                    } else if (fields.get(field).equals(FieldType.TIMESTAMP)) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        map.put(field, format.parse(value));
                    } else if (fields.get(field).equals(FieldType.BOOLEAN)) {
                        if (value.equals("1")) {
                            map.put(field, true);
                        } else {
                            map.put(field, false);
                        }
                    } else if (fields.get(field).equals(FieldType.ENUM)) {
                        map.put(field, value);
                    } else if (fields.get(field).equals(FieldType.IN)) {
                        map.put(field, "(" + value + ")");
                    }else {
                        map.put(field, value);
                    }
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }
}
